|
|
|
|
|
|
|
Listing 3.3. Opening the database and creating the Dynaset. |
|
|
|
|
|
|
|
|
Private Sub Form_Load()
'
' creating dynaset-type recordsets
'
Dim db As Database ' the database object
Dim rs As Recordset ' the recordset object
'
' create local variables
Dim strDBName As String
Dim strRSName As String
'
' initialize the variables
strDBName = App.Path & "\..\data\books5.mdb"
strRSName = "Titles"
'
' create the objects
Set db = DBEngine.OpenDatabase(strDBName)
Set rs = db.OpenRecordset(strRSName, dbOpenDynaset)
'
End Sub |
|
|
|
|
|
|
|
|
There are two added lines in Listing 3.3. The first added line opens the BOOKS5.MDB database and sets the Visual Basic database object db to point to the database. This gives your Visual Basic program a direct link to the selected database. |
|
|
|
|
| |
|
|
|
|
Note that this database object was created using the OpenDatabase method of the DBEngine object. The DBEngine is covered in greater detail on Day 9. |
|
|
|
|
|
|
|
|
|
|
Now you can use the db data object to represent the open database in all other Visual Basic code in this program. The second line creates a Dynaset-type Recordset object that contains all the records in the Titles table. The Visual Basic rs object is set to point to this set of records. Notice that the OpenRecordset method is applied to the db Database object. |
|
|
|
|
| |
|
|
|
|
Notice that these last two lines of code use the Set keyword. This Visual Basic keyword is used to initialize all programming objects. You might think that you could perform the same task using the following code line: |
|
|
|
|
|
|
|
|
Rs = db.OpenRecordSet(strRSName,dbOpenRecordset) |
|
|
|
|
|
|
|